#!/usr/bin/env perl

use strict;
use 5.010;

# main

# test unless, unless(expr) = if(!expr)
my $fred = "this_is_a_var_name";
unless ($fred =~ /^[A-Z_]\w*$/i)
{
    printf "the value of \$fred doesn't look like a Perl identifier name.\n";
}
else
{
    printf "the value of \$fred looks like a Perl identifier name.\n";
}

$fred = "this is not a var name";
unless ($fred =~ /^[A-Z_]\w*$/i)
{
    printf "the value of \$fred doesn't look like a Perl identifier name.\n";
}
else
{
    printf "the value of \$fred looks like a Perl identifier name.\n";
}

# test until, until(expr) = while(!expr), until will exist its loop until the expr is true
my ($var1, $var2) = (1, 10);
until($var1 > $var2)
{
   say "in until \$var1 = $var1";
   $var1 += 1;
   my $var3 = $var1++;
   say "$var3, $var1";
   my $var4 = ++$var1;
   say "$var4, $var1";
}

# test 'last', 'next', 'redo'
for(my $i = 0; $i < 10; ++$i)
{
    if($i == 5)
    {
        say "\$i == 5 run next";
        next;
    }
    if($i == 8)
    {
        say "\$i == 8 run last";
        last;
    }
    say $i;
}

foreach my $i (1..10)
{
    if($i == 5)
    {
        say "\$i == 5 run next";
        next;
    }
    if($i == 8)
    {
        say "\$i == 8 run last";
        last;
    }
    say $i;
}

# redo will go to the top of the loop without increase iterate variable.
foreach my $i (1..5)
{
    say $_;
    say "Type the word '$_':";
    chomp(my $try =  <STDIN> );
    if($try != $i)
    {
        say "error \$try != \$i";
        redo;
    }
}

# here is label, 'LINE' is a label, break label with 'last'
LINE: while(<>)
{
    foreach (split)
    {
        last LINE if /__END__/;
        say $_;
    }
}



